package com.ruoyi.mp.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.system.PcProduct;
import com.ruoyi.db.domain.web.MpClientCart;
import com.ruoyi.db.domain.web.MpClientSubCart;
import com.ruoyi.db.mapper.system.PcProductMapper;
import com.ruoyi.db.mapper.web.MpClientCartMapper;

import com.ruoyi.db.mapper.web.MpClientSubCartMapper;
import com.ruoyi.mp.service.IMpClientCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * 用户购物车Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-02
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class MpClientCartServiceImpl implements IMpClientCartService
{
    @Autowired
    private MpClientCartMapper mpClientCartMapper;
    @Autowired
    private MpClientSubCartMapper mpClientSubCartMapper;
    @Autowired
    private PcProductMapper pcProductMapper;

    /**
     * 查询用户购物车
     * 
     * @param cartId 用户购物车主键
     * @return 用户购物车
     */
    @Override
    public MpClientCart selectMpClientCartByCartId(Long cartId)
    {
        return mpClientCartMapper.selectMpClientCartByCartId(cartId);
    }

    /**
     * 查询用户购物车列表
     * 
     * @param mpClientCart 用户购物车
     * @return 用户购物车
     */
    @Override
    public List<MpClientCart> selectMpClientCartList(MpClientCart mpClientCart)
    {
        return mpClientCartMapper.selectMpClientCartList(mpClientCart);
    }

    /**
     * 新增用户购物车
     * 
     * @param mpClientCart 用户购物车
     * @return 结果
     */
    @Override
    public void insertMpClientCart(MpClientSubCart subCart,Long clientId)
    {
        //查询用户是否已经有购物车数据
        MpClientCart cart = new MpClientCart();
        cart.setClientId(clientId);
        List<MpClientCart > myCarts = mpClientCartMapper.selectMpClientCartList(cart);
        if(myCarts.size()>0){
            //更新子购物车产品信息,同产品加数量
            MpClientCart myCart =myCarts.get(0);
            MpClientSubCart child = new MpClientSubCart();
            child.setCartId(myCart.getCartId());
            List<MpClientSubCart> childs = mpClientSubCartMapper.selectMpClientSubCartList(child);
            Boolean havPro = false;
            for (MpClientSubCart subCart1: childs) {
                if(subCart1.getProId()==subCart.getProId()){
                    subCart1.setProCount(subCart1.getProCount()==null?0:subCart1.getProCount()+subCart.getProCount());
                    mpClientSubCartMapper.updateMpClientSubCart(subCart1);
                    havPro=true;
                }
            }
            if(!havPro){
                subCart.setCartId(myCart.getCartId());
                subCart.setCreateTime(new Date());
                mpClientSubCartMapper.insertMpClientSubCart(subCart);
            }

        }else{
            MpClientCart insCart = new MpClientCart();
            insCart.setClientId(clientId);
            insCart.setCreateTime(new Date());
            if( mpClientCartMapper.insertMpClientCart(insCart)>0){
                subCart.setCartId(insCart.getCartId());
                subCart.setCreateTime(new Date());
                mpClientSubCartMapper.insertMpClientSubCart(subCart);
            }
        }
    }

    /**
     * 修改用户购物车
     * 
     * @param mpClientCart 用户购物车
     * @return 结果
     */
    @Override
    public void updateMpClientCart(MpClientSubCart subCart)
    {
        if(subCart.getProCount()==null||subCart.getProCount()<=0){
            throw new ServiceException("数量不能再少了");
        }
        PcProduct pro = pcProductMapper.selectPcProductByProId(subCart.getProId());
        if(pro.getStorageAmount()<subCart.getProCount()){
            throw new ServiceException("该产品库存不足");
        }
        mpClientSubCartMapper.updateMpClientSubCart(subCart);
    }

    /**
     * 批量删除用户购物车
     * 
     * @param cartIds 需要删除的用户购物车主键
     * @return 结果
     */
    @Override
    public void deleteMpClientCartByCartIds(Long[] subCartIds,Long clientId)
    {
        int i = mpClientSubCartMapper.deleteMpClientSubCartBySubCartIds(subCartIds);
        if(i>0){
            //查询用户是否已经有购物车数据
            MpClientCart cart = new MpClientCart();
            cart.setClientId(clientId);
            List<MpClientCart > myCarts = mpClientCartMapper.selectMpClientCartList(cart);
            if(myCarts.size()>0){
                MpClientCart myCart =myCarts.get(0);
                //查询购物车下是否存在商品,不存在，删除购物车
                MpClientSubCart child = new MpClientSubCart();
                child.setCartId(myCart.getCartId());
                List<MpClientSubCart> childs = mpClientSubCartMapper.selectMpClientSubCartList(child);
                if(childs.size()<=0){
                    mpClientCartMapper.deleteMpClientCartByCartId(myCart.getCartId());
                }
            }
        }
    }

    /**
     * 删除用户购物车信息
     * 
     * @param cartId 用户购物车主键
     * @return 结果
     */
    @Override
    public int deleteMpClientCartByCartId(Long cartId)
    {
        return mpClientCartMapper.deleteMpClientCartByCartId(cartId);
    }

    @Override
    public MpClientCart selectMpClientCartByClientId(Long clientId) {
        MpClientCart cart = new MpClientCart();
        cart.setClientId(clientId);
        List<MpClientCart> list = mpClientCartMapper.selectMpClientCartList(cart);
        if(list.size()>0){
            MpClientCart myCart =list.get(0);
            MpClientSubCart child = new MpClientSubCart();
            child.setCartId(myCart.getCartId());
            List<MpClientSubCart> childs = mpClientSubCartMapper.selectMpClientSubCartList(child);
            myCart.setChilds(childs);
            return myCart;
        }else{
            return null;
        }
    }
}
